package cn.edu.nuc.fourth.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.edu.nuc.fourth.model.CheckSchool;
import cn.edu.nuc.fourth.model.Major;
import cn.edu.nuc.fourth.model.Project;
import cn.edu.nuc.fourth.model.School;
import cn.edu.nuc.fourth.services.interfaces.CheckService;

@Controller
@RequestMapping("/check")
public class CheckController {

	@Autowired
	private CheckService service;

	@RequestMapping("/checkschool")
	public String schoolcheck(Integer userid, String date, Model model) {

		CheckSchool cs = service.checkSchool(-1, "2016");
		model.addAttribute("school", cs.getSchool());
		model.addAttribute("major", cs.getMajor());
		return "qxadmin/checkSchool";
	}

	@RequestMapping("/docheckschool")
	public String docheckSchool(Major major, School school, String userid, Model model) {

		CheckSchool cs = service.dochekcSchool(major, school, "-1");
		model.addAttribute("school", cs.getSchool());
		model.addAttribute("major", cs.getMajor());
		return "qxadmin/checkSchool";
	}

	@RequestMapping("/checkproject")
	public String checkProject(String userid, String date, Model model) {

		List<Project> list = service.checkProject("-1", "2016");

		for (int i = 0; i < list.size(); i++) {

			Project p = list.get(i);
			if (p.getProjecttype().equals("main")) {
				model.addAttribute("main", p);
			}
			if (p.getProjecttype().equals("province")) {

				model.addAttribute("province", p);
			}
			if (p.getProjecttype().equals("city")) {

				model.addAttribute("city", p);
			}
			if (p.getProjecttype().equals("onther")) {

				model.addAttribute("onther", p);
			}
		}
		return "qxadmin/checkProject";
	}

	@RequestMapping(value = "/docheckproject", method = RequestMethod.POST)
	public String docheckProject(Project project, Model model) {

		List<Project> list = service.docheckProject(project);
		for (int i = 0; i < list.size(); i++) {

			Project p = list.get(i);
			if (p.getProjecttype().equals("main")) {
				model.addAttribute("main", p);
			}
			if (p.getProjecttype().equals("province")) {

				model.addAttribute("province", p);
			}
			if (p.getProjecttype().equals("city")) {

				model.addAttribute("city", p);
			}
			if (p.getProjecttype().equals("onther")) {

				model.addAttribute("onther", p);
			}
		}
		return "qxadmin/checkProject";
	}
	
	@RequestMapping(value = "/checkdownload")
	public ResponseEntity<byte[]> download(HttpServletRequest request, String path, String filename)
			throws IOException {

		File file = new File(path, filename);
		HttpHeaders header = new HttpHeaders();
		String fname = new String(filename.getBytes("UTF-8"), "iso-8859-1");
		header.setContentDispositionFormData("attachment", fname);
		header.setContentType(MediaType.APPLICATION_OCTET_STREAM);

		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), header, HttpStatus.CREATED);
	}

	@InitBinder("school")
	public void school(WebDataBinder binder) {

		binder.setFieldDefaultPrefix("school.");
	}

	@InitBinder("major")
	public void major(WebDataBinder binder) {

		binder.setFieldDefaultPrefix("major.");
	}
}
